var app = require('express')();
var http = require('http').Server(app);
var session = require('express-session');


var parseData = require('./parseData');

app.use(session({
    secret: 'keyboard cat',
    resave: false,
    saveUninitialized: true,
    cookie: { secure: false }
}));

//设置跨域访问
app.all("*", function (req, res, next) {
    //设置允许跨域的域名，*代表允许任意域名跨域
    res.header("Access-Control-Allow-Origin", "*");
    //允许的header类型
    res.header("Access-Control-Allow-Headers", "content-type");
    //跨域允许的请求方式 
    res.header("Access-Control-Allow-Methods", "DELETE,PUT,POST,GET,OPTIONS");
    if (req.method.toLowerCase() == 'options')
        res.send(200);  //让options尝试请求快速结束
    else
        next();
});

app.get('/getServerInfo', function (req, res) {
    // res.send('<h1>Hello world</h1>');
    var params = {code:0,data:111}
    res.send(JSON.stringify(params));
});

app.get('/', function (req, res) {
    res.send('<h1>Hello world</h1>');
    // var params = {code:0,data:111}
    // res.send(JSON.stringify(params));

    // session测试
    // req.session.mytestcode = '1111';
    // console.log('req.session');

    let data = {
        username: 'qqq',
        password: '111'
    }
    let e = parseData.encode('Msg.RegR', data);
    console.log('eeeee', e.length, e);
    // let q = parseData.decode('Msg.RegR',e);
    // console.log('qqqqq',q);

    // let arrayBuffer = new ArrayBuffer(4+e.length);
    // let dataView = new DataView(arrayBuffer);
    // dataView.setInt32(0,1001)
    // for(let i=0;i<e.length;i++){
    //     dataView.setUint8(i+4,e[i]);
    // }
    // console.log('-------',arrayBuffer);

    // let dataView1 = new DataView(arrayBuffer,0,4);
    // let mid = dataView1.getInt32(0);
    // console.log('mid>>',mid);

    // var buf = new Uint8Array(arrayBuffer,4);
    // let demsg = parseData.decode('Msg.RegR',buf);
    // console.log('demsg>>',demsg);

    let buf = parseData.str2ab('Msg.RegR');
    console.log('buf>>>>', buf, buf.length);

    buf = Array.prototype.slice.call(new Uint8Array(buf));
    console.log('buf222>>>>', buf, buf.length);

    let str = parseData.ab2str(buf);
    console.log('str>>>>', str);

    let arrayBuffer = new ArrayBuffer(buf.length + e.length);
    console.log('arrayBuffer>>', arrayBuffer.length);
    let dataView = new DataView(arrayBuffer);
    for (let i = 0; i < buf.length; i++) {
        dataView.setUint8(i, buf[i]);
    }
    for (let i = 0; i < e.length; i++) {
        console.log('iiiiii>>>', i)
        dataView.setUint8(i + buf.length, e[i]);
    }
    console.log('-------', arrayBuffer);

    // let dataView1 = new Uint8Array(arrayBuffer, 0, buf.length);
    // dataView1 = Array.prototype.slice.call(new Uint8Array(dataView1));
    // console.log('dataView1>>>>', dataView1);

    let dataView1 = arrayBuffer;
   
    let str1 = parseData.ab2str(dataView1);
    console.log('str1>>>>', str1);


    var buf33 = new Uint8Array(arrayBuffer,buf.length);
    let demsg = parseData.decode('Msg.RegR',buf33);
    console.log('demsg>>',demsg,typeof demsg);
    console.log('demsg222>>',demsg.username);
});

http.listen(3000, function () {
    console.log('Server running at http://127.0.0.1:3000/');
});

const WebSocket = require('ws');
const ws = new WebSocket.Server({ port: 3001 });
let clients = [];

ws.on('connection', function (client) {
    clients.push(client);
    client.on('message', function (data) {
        console.log('data', data);

        // let q = parseData.decode('Msg.RegR',data);
        // console.log('qqqqqq',q);



    });

    client.on('close', () => {
        let index = clients.indexOf(client);
        if (index !== -1) {
            clients.splice(index, 1);
            console.log('clinet closed');
        }
    });
});